package com.mach.platform.sprider.dev;

import com.mach.platform.sprider.Conf;
import com.mach.platform.util.SqlHelper;

import java.io.IOException;

/*
    取得变频器数据抓取器,一个对象针对一个采集模块
 */
public class TransducerPlSNDDev extends TransducerPlDev {

    protected String devName;

    protected SqlHelper sqls;

    public TransducerPlSNDDev(String ip, Integer port, Integer devID, String devName) {
        super(ip,port,devID, devName);

        this.sqls  = Conf.getSqlHelper();

        this.devName = devName;
    }


    /*
      取变频器频率
   */
    public Float getPl() {
        Float result = null;

        try {
//            1C52 1C53 2136 0C82
            byte[] repData = this.getDataRtu(plAddrGet,1);
            if(repData.length <1)return null;
            byte[] data = ByteUtil.subData(repData,3,2);
            data = ByteUtil.reverse(data);
//            result = ByteUtil.getFloat(data);
            result = ByteUtil.getShort(data)/10.0f;
            System.out.println("pl=" + result);
        } catch (Exception e) {
            System.out.println("excep devId:" + this.devName + " " + e.getMessage());
        }


        return result;
    }

    public void setPl(Integer pl) throws IOException {
        this.setDataRtu(plAddrSet, pl);
    }


    public Short getCmd() {
        Short result = null;

        try {
//            1C52 1C53 2136 0C82
            byte[] repData = this.getDataRtu(cmdAddrGet,1);
            if(repData.length <1)return null;
            byte[] data = ByteUtil.subData(repData,3,2);
            UU.printData(data);
            data = ByteUtil.reverse(data);
            result = ByteUtil.getShort(data);
            System.out.println("cmd=" + result);
        } catch (Exception e) {
            System.out.println("excep devId:" + this.devName + " " + e.getMessage());
        }


        return result;
    }

    public void setCmd(Integer cmdCode) throws IOException {
        this.setDataRtu(cmdAddrSet, cmdCode);
    }

    //0x0C82
    private Integer plAddrSet = 8502;//设定频率
    private Integer plAddrGet = 3202;//输出频率

    private Integer cmdAddrSet = 8501;//输出CMD
    private Integer cmdAddrGet = 3201;//当前ETA

//    public static void main(String[] args){
//        TransducerPlSNDDev td = new TransducerPlSNDDev("192.168.3.91",8000,5,"8#变频器（PA2工艺16H）");
//        Float pl = td.getPl();
//        System.out.println("pl="+pl);
//    }

    public static void main(String[] args) throws IOException {
//     findAll(args);
        //05 05 13 17
        TransducerPlSNDDev dev  = new TransducerPlSNDDev("192.168.1.201",30005 ,2,"");
//        dev.setCmd(0x06);
//        dev.setCmd(0x07);
//        dev.setCmd(0x0F);

//        dev.setPl(-200);

        //start
        dev.setCmd(128);
        sleep(5000);
        dev.getCmd();

        dev.setCmd(0x06);
        sleep(3000);
        dev.getCmd();

        dev.setCmd(0x07);
        sleep(3000);
        dev.getCmd();

        boolean isBreak = true;
        while(isBreak){
            dev.setPl(50);
            dev.setCmd(0x0F);//0x0F 2063
            sleep(1000);
            dev.getCmd();
        }
        //end

        sleep(10000);
        dev.getCmd();
        dev.getDataRtu(7121,1);
        dev.getPl();

//        dev.getCmd();
//        dev.getDataRtu(7121,1);
//        dev.getPl();

    }

    public static  void sleep(long ms){
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void findAll(String[] args) throws IOException {
        int endIx = 3;
        for(int j = 30001; j<= 30017; j+=4){
            for(int i = 2; i <= 3; ++i){
                readIdById("192.168.1.201",j,i,"XXXX" );
            }

        }
    }
    public static void readIdById(String ip ,Integer port, Integer id,String devName) throws IOException {
        TransducerPlSNDDev dev =  new TransducerPlSNDDev(ip,port,id,devName);
        byte[] data = dev.readSth(0x0C82, 2, 3);
        //U.printData(data);
//        dev.setPl(20);

    }


}
